package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dto.OrderSettingDTO;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;


    /**
     * 添加预约设置
     *
     * @param orderSettings 预约设置对象集合
     */
    @Override
    @Transactional
    public void saveOrderSetting(List<OrderSetting> orderSettings) {
        //获取添加的日期
        String date = null;
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            date = String.format("%tY", orderDate) + "-" + String.format("%tm", orderDate);
        }
        //查询是否有相关日期的预约设置
        List<OrderSetting> settings = orderSettingDao.selectListOrderSetting(date);
        //有相关日期的预约设置，全都删除
        if (settings.size() > 0){
            //调用持久层，先删除对应日期的预约信息
            orderSettingDao.deleteByDate(date);
        }
        //不存在相关预约设置，直接添加预约设置
        orderSettingDao.insert(orderSettings);
    }

    /**
     * 指定日期，可预约人数大于已预约人数
     * @param orderDate 预约日期
     */
    @Override
    public Integer countOrderSettingByDay(Date orderDate) {
        return orderSettingDao.selectCountOrderSettingByDay(orderDate);
    }

    /**
     * 指定日期，查询对应日期是否存在预约设置
     * @param orderDate 预约日期
     */
    @Override
    public Integer countCanOrder(Date orderDate) {
        return orderSettingDao.selectCountCanOrder(orderDate);
    }

    /**
     * 查询 预约设置
     *
     * @param year  年
     * @param month 月
     */
    @Override
    public List<OrderSettingDTO> getOrderSettingList(String year, String month) {
        //组装参数
        String date = Integer.parseInt(month) < 10 ? year + "-0" + month : year + "-" + month;
        //查询获取所有预约数据
        List<OrderSetting> OrderSettingList = orderSettingDao.selectListOrderSetting(date);
        //创建一个存储OrderSettingDTO对象的List集合
        ArrayList<OrderSettingDTO> orderSettingDTOList = new ArrayList<>();
        //遍历
        for (OrderSetting orderSetting : OrderSettingList) {
            //创建DTO对象
            OrderSettingDTO orderSettingDTO = new OrderSettingDTO();
            //获取这个月中的某一天
            int day = Integer.valueOf(String.format("%te", orderSetting.getOrderDate()));
            //设置当天日期
            orderSettingDTO.setDate(day);
            //获取总预约人数
            orderSettingDTO.setNumber(orderSetting.getNumber());
            //获取当前预约人数
            orderSettingDTO.setReservations(orderSetting.getReservations());
            //添加到集合中
            orderSettingDTOList.add(orderSettingDTO);
        }
        //返回数据
        return orderSettingDTOList;
    }

    /**
     * 修改预约设置
     *
     * @param orderSetting 参数
     */
    @Override
    @Transactional
    public void updateNumberByDay(OrderSetting orderSetting) {
        //先查询当天是否可以预约
        Integer canOrder = orderSettingDao.selectCountCanOrder(orderSetting.getOrderDate());
        //不能预约，先把当天设置为可预约
        if (canOrder==0){
            //添加预约设置
            ArrayList<OrderSetting> orderSettingList = new ArrayList<>();
            //设置默认已预约人数为0
            orderSetting.setReservations(0);
            orderSettingList.add(orderSetting);
            //添加预约设置
            orderSettingDao.insert(orderSettingList);
        }
        //修改可预约人数前，查询已预约人数， 可预约人数>=已预约人数
        Integer reservations = orderSettingDao.selectOneReservations(orderSetting);
        //判断可预约人数和已预约人数
        if (orderSetting.getNumber() < reservations) {
            throw new BusinessRuntimeException("可预约人数不能小于已预约人数！");
        }

        orderSettingDao.update(orderSetting);
    }

}
